package com.hy.dao.impl;

import com.hy.bean.Consumer;
import com.hy.dao.ConsumerDao;
import com.hy.utils.DBManager;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class ConsumerDaoImpl implements ConsumerDao {
    private QueryRunner qr = new QueryRunner();
    @Override
    public Consumer queryById() {
        return null;
    }

    public List<Consumer> queryAll() {
        Connection connection = null;
        String sql = "select * from consumer";

        try {
            connection = DBManager.getConnection();
            return qr.query(connection, sql, new BeanListHandler<>(Consumer.class));
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                DbUtils.close(connection);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public long count(Consumer consumer) {
        return 0;
    }

    @Override
    public int insert(Consumer consumer) {
        return 0;
    }

    @Override
    public int insertBatch(List<Consumer> entities) {
        return 0;
    }

    @Override
    public int insertOrUpdateBatch(List<Consumer> entities) {
        return 0;
    }

    @Override
    public int update(Consumer consumer) {
        return 0;
    }

    @Override
    public int deleteById() {
        return 0;
    }

    @Override
    public Consumer selectUsernameBy(String username, String pwd) {
        Consumer consumer = null;
        try {
            Connection conn = DBManager.getConnection();

            String sql = "select * from consumer where disable_flag = 0 and username = ? and pwd = ?";

           consumer = qr.query(conn, sql, new BeanHandler<>(Consumer.class, new BasicRowProcessor(new GenerousBeanProcessor())), username, pwd);

           DbUtils.close(conn);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return consumer;
    }

}
